<?php

namespace app\api\controller;

use app\common\controller\Api;
use Elasticsearch\ClientBuilder;
use think\Controller;
use think\Db;
use think\Log;
use think\Request;

class Process extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function searchFiledata(Request $request){
        $param = $request->param();
//        $param = $param['inmap'];
//        $baseParam = $param['baseParam']; //公共请求参数
        $search = $param['search'];
        $lockcode = $param['lockcode']; //锁内码

        if(((string)$lockcode)[0] == '2' && !in_array($lockcode,['200000124','200001329','200000120','200000517'])){  //扫码的用户锁号不支持用次功能
            return json_encode([
                'success'=>false,
                'status'=>-1,
                'message'=>'功能暂时限制，请联系400-800-5201寻找解决方案。',
                'version'=>'',
                'results'=>'',
            ]);
        }

        $data = Db::name('user_software_file')
            ->whereNull('deletetime')
            ->where('status','normal')
            ->where('hashcode',$search)
            ->field('id,name as filename,file as url')
            ->find();
        if($data){
            $aliossurl = get_addon_config('alioss')['cdnurl'];
            $data['url'] = $aliossurl.$data['url'];

            //找到了就扣除对应锁号的条数
            $setdecparam['Appid'] = 'ZJ361702';
            $setdecparam['lockcode'] = $lockcode;
            $setdecparam['filename'] = $data['filename'];
            $setdecparam['fileid'] = $data['id'];
            $setdecmessage = $this->setdecsoftwarefile_download($setdecparam);
            if($setdecmessage['status'] != 1){
                return json_encode([
                    'success'=>false,
                    'status'=>-1,
                    'message'=>$setdecmessage['message'],
                    'version'=>'',
                    'results'=>'',
                ]);
            }

            Db::name('user_lockcode_searchfilelog')->insert([
                'lockcode'=>$lockcode,
                'num'=>1,
                'fileid'=>$data['id'],
                'createtime'=>time(),
                'mark'=>'搜索文件减少1次',
            ]);

            unset($data['id']);
            return json_encode([
                'success'=>true,
                'status'=>200,
                'message'=>'',
                'version'=>'',
                'results'=>$data,
            ]);
        }else{
            return json_encode([
                'success'=>true,
                'status'=>-2,
                'message'=>'',
                'version'=>'',
                'results'=>'',
            ]);
        }
    }

    //扣除条数
    function setdecsoftwarefile_download($param){
        $url = "http://xinbiaogan.cn:8080/api/process/getlockcodeinfo?Appid=".$param['Appid']."&lockcode=".$param['lockcode']
            ."&filename=".$param['filename']."&fileid=".$param['fileid'];

        $header = [
            'Content-Type: application/json'
        ];

        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);

        $response = curl_exec($curl);
        curl_close($curl);
        // 处理响应
        return json_decode($response,true);
    }
}